﻿using MESMian.Common;
using MESMian.Forms;
using MESMian.Forms.DeviceSpec;
using MESMian.Forms.ProductionCategory;
using MESMian.Forms.ProductionSpec;

namespace MESMian
{
    public partial class MainForm : Form
    {
        System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
        public MainForm()
        {
            InitializeComponent();
            this.Size = new Size(1100, 600);
            Load += MainForm_Load;
            timer.Tick += Timer_Tick;
            this.FormClosing += MainForm_FormClosing;
            Resize += MainForm_Resize;
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void MainForm_Resize(object? sender, EventArgs e)
        {
            if (panelForm.Controls.Count > 0)
            {
                Form form = panelForm.Controls[0] as Form;
                switch (form.Name)
                {
                    case "TeamsList":
                        班组信息ToolStripMenuItem.PerformClick();
                        break;
                    case "ProductsList":
                        产品信息ToolStripMenuItem1.PerformClick();
                        break;
                    case "DeviceSpecList":
                        设备规格ToolStripMenuItem.PerformClick();
                        break;
                    case "ProductionCategoryList":
                        产品类型ToolStripMenuItem.PerformClick();
                        break;
                    case "ProductionSpecList":
                        产品规格ToolStripMenuItem.PerformClick();
                        break;
                    default:
                        break;
                }
            }
        }

        private void MainForm_FormClosing(object? sender, FormClosingEventArgs e)
        {
            timer.Stop();
        }

        private void Timer_Tick(object? sender, EventArgs e)
        {
            tsslCurrentTime.Text = $"当前时间：{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}";
        }

        private void MainForm_Load(object? sender, EventArgs e)
        {
            tsslCurrentUser.Text = $"当前账号：{LoginInfo.UserName}";
            timer.Start();
        }

        private void ToggleForms(Form form)
        {
            // 清除panel中的控件
            panelForm.Controls.Clear();
            // 设置窗体的样式和嵌入属性
            form.FormBorderStyle = FormBorderStyle.None;
            form.WindowState = FormWindowState.Maximized;
            form.TopLevel = false; // 非顶级窗体
            // 添加到panel中
            panelForm.Controls.Add(form);
            // 设置标题
            lblName.Text = form.Text!.ToString();
            // 显示窗体
            form.Show();

        }


        private ProductsList productsList;
        private void 产品信息ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ToggleForms(new ProductsList());
        }

        private TeamsList teamsList;
        private void 班组信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleForms(new TeamsList());
        }

        private StoreList storeList;
        private void 仓库信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleForms(new StoreList());
        }

        private void 产品类型ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleForms(new ProductionCategoryList());
        }

        private void 设备规格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleForms(new DeviceSpecList());
        }

        private void 产品规格ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ToggleForms(new ProductionSpecList());
        }
    }
}
